<?php
include_once dirname(__FILE__)."/../../model/config.php";
		$_phase=new phase();
if(isset($_POST["operation"])){
if($_POST["operation"]=="INSERT"){
		if(isset($_POST["code_phase"])){$_phase->set_code_phase($_POST["code_phase"]);}
		if(isset($_POST["lib_phase"])){$_phase->set_lib_phase($_POST["lib_phase"]);}
		if(isset($_POST["code_unite"])){$_phase->set_code_unite($_POST["code_unite"]);}
		if(isset($_POST["code_etat"])){$_phase->set_code_etat($_POST["code_etat"]);}
		if(isset($_POST["date"])){$_phase->set_date($_POST["date"]);}
		if(isset($_POST["observation"])){$_phase->set_observation($_POST["observation"]);}
		$res=$_phase->INSERT(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}
elseif($_POST["operation"]=="DELETE"){
		if(isset($_POST["code_phase"])){
		$_phase->set_code_phase($_POST["code_phase"]);
		$res=$_phase->DELETE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
		}
}
elseif($_POST["operation"]=="UPDATE"){
		if(isset($_POST["code_phase"])){$_phase->set_code_phase($_POST["code_phase"]);}
		if(isset($_POST["lib_phase"])){$_phase->set_lib_phase($_POST["lib_phase"]);}
		if(isset($_POST["code_unite"])){$_phase->set_code_unite($_POST["code_unite"]);}
		if(isset($_POST["code_etat"])){$_phase->set_code_etat($_POST["code_etat"]);}
		if(isset($_POST["date"])){$_phase->set_date($_POST["date"]);}
		if(isset($_POST["observation"])){$_phase->set_observation($_POST["observation"]);}
		$res=$_phase->UPDATE(); 

	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('success' => 'true','result' => "$res" ));
	  	
	  }
}

}
else{
	$constraint='';$limit=0;$start=0;$pagination='';
	if(isset($_POST["code_phase"]) && !empty($_POST["code_phase"])){
		$constraint.=" AND code_phase='".$_POST["code_phase"]."'";
	}

	if(isset($_POST["lib_phase"]) && !empty($_POST["lib_phase"])){
		$constraint.=" AND lib_phase='".$_POST["lib_phase"]."'";
	}

	if(isset($_POST["code_unite"]) && !empty($_POST["code_unite"])){
		$constraint.=" AND code_unite='".$_POST["code_unite"]."'";
	}

	if(isset($_POST["code_etat"]) && !empty($_POST["code_etat"])){
		$constraint.=" AND code_etat='".$_POST["code_etat"]."'";
	}

	if(isset($_POST["date"]) && !empty($_POST["date"])){
		$constraint.=" AND date='".$_POST["date"]."'";
	}

	if(isset($_POST["observation"]) && !empty($_POST["observation"])){
		$constraint.=" AND observation='".$_POST["observation"]."'";
	}

	if(isset($_POST['limit'])){$limit=$_POST['limit'];}
	if(isset($_POST['start'])){$start=$_POST['start'];}
	if($limit>0){
		$pagination.=" LIMIT  $start,$limit"; 
}
		$res=$_phase->SELECT($constraint,$pagination); 

		 $data=array();
		  while($row=$_phase->fetch_object()){
		  	$data[]=$row;
	  	  }
  	 	 
		
	   if($res!="success"){
		echo json_encode(array('success' => 'false','result' => "$res" ));
	  }else{
	  	echo json_encode(array('totalcount' => count($data),'result' => $data ));
	  	
	  }}
?>